'use strict';
var co = require('co');
var parse = require('co-body');
var error = require('koa-error');
var jwt = require('jsonwebtoken');
var kjwt = require('koa-jwt');
var User = require('../model/user');

module.exports = {
    list: function *() {
        try{
            var access_token = this.request.query.access_token || this.request.header.access_token;
            console.log(access_token);
            var decoded = jwt.verify(access_token,'supersecret');
            if(decoded.FID>0){
                var data = yield User.findAll();
                this.status = 200;
                this.body = JSON.stringify({success: true, message: 'listed',user:data});
            }
        }catch(error){
            this.status = 500;
            this.body = {status: 'failed', error: error};
        }
    },
    getuser : function *(){
    try{
        var access_token = this.request.query.access_token || this.request.header.access_token;
        var FID = this.request.query.FID;
        var decoded = jwt.verify(access_token,'supersecret');
        if(decoded.FID>0) {
            this.status = 200;
            var data = yield User.findOne({
                where: {FID: FID},
                attributes: ['FID', 'FNumber','FName','FType']});
            this.body = JSON.stringify({success: true, message: 'getted',userlist:data});
        }
    }catch(error){
        this.status = 500;
        this.body = {status: 'failed', error: error};
    }
},
    createuser: function *() {
        try{
            var access_token = this.request.query.access_token || this.request.header.access_token;
            var decoded = jwt.verify(access_token, 'supersecret');

            if(decoded.FID>0){
                var count = yield User.count();
                //var body = yield parse.text(this);
                //console.log(body);
                console.log(count);
                console.log(this.request.body);
                this.body = this.request.body;
                yield User.create(this.request.body);

                var FID = yield User.findOne();
                this.body = JSON.stringify({success: true, message: 'getted',FID:FID});
            }

        }catch(error){
            this.status = 500;
            this.body = {status: 'failed', error: error};
        }
        //this.body = yield User.findAll();
    },
    getUserById: function *() {
        this.body = yield User.findAll({
            where: {
                FID: this.params.id
            }
        });
    },
    createUser: function *() {

    },
    editUser: function *() {

    },
    deleteUser: function *() {
        yield User.destroy({
            where: {
                FID: this.params.id
            }
        });
        this.body = {status: 'deleted'}
    },
    verifyLogin: function (username, password) {
        return User.findOne({
            where: {
                FName: username,
                FPassword: password
            }
        });
    },
    verifyUserById: function (id) {
        return User.findOne({
            where: {
                FID: id
            }
        });
    }

};